var express = require("express");
var app = express();

var mongoose = require("mongoose");
mongoose.connect("mongodb://127.0.0.1/task");
var start = require("./models/start.js").start;

app.use(express.static("static"));

app.get("/", function(req, res) {
        res.send("欢迎来到任务列表")
    })
    // var task = [{
    //         name: "天空一号",
    //         time: 2017,
    //         star: 5,
    //         finish: "未完成"
    //     }, {
    //         name: "抗震救灾",
    //         time: 2018,
    //         star: 5,
    //         finish: "未完成"
    //     }, {
    //         name: "大学生就业",
    //         time: 2017,
    //         star: 4,
    //         finish: "未完成"
    //     }, {
    //         name: "共享单车",
    //         time: 2017,
    //         star: 3,
    //         finish: "合格"
    //     }]
var getData = function(req, res, finish) {
    var page = req.query.page;
    var pageCount = req.query.pageCount;
    var queryObj = {};
    // finish: { $in: ["未合格", "合格", "良好", "优秀"] }, 
    if (finish) {
        queryObj = { finish: finish };
    }
    start.count(queryObj, function(err, docs) {
        var length = docs;
        var nowTotalPage = Math.ceil(length / pageCount);
        if (page >= nowTotalPage) {
            page = nowTotalPage - 1;

        } else if (page < 0) {
            page = 0;
        }
        if (length <= 0) {
            res.send({
                status: 0,
                length: 0,
                totalPage: 1,
                page: 0
            });
        } else {
            if (finish) {
                var skip = page * pageCount;
                start.find(queryObj, null, { skip: skip, limit: pageCount }, function(err, docs) {
                    res.send({
                        status: 0,
                        length: length,
                        totalPage: nowTotalPage,
                        page: page,
                        data: docs
                    });
                })
            } else {
                var skip = page * pageCount;
                start.find({}, null, { skip: skip, limit: pageCount }, function(err, docs) {
                    res.send({
                        status: 0,
                        length: length,
                        totalPage: nowTotalPage,
                        page: page,
                        data: docs
                    });
                })
            }

        }
    })
}


app.get("/getTaskData", function(req, res) {
    getData(req, res, "未完成");
});

app.get("/getTaskDatas", function(req, res) {
    getData(req, res);
});
app.get("/del", function(req, res) {
    var id = req.query.id;
    start.remove({ _id: id }, function(err) {
        getData(req, res);
    })
})
app.get("/add", function(req, res) {
    var newTask = req.query.newTask;
    start.create(newTask, function(err) {
        getData(req, res);
    })
})
app.get("/changeStar", function(req, res) {
    var id = req.query.id;
    var starIndex = req.query.starIndex;
    start.update({ _id: id }, { star: starIndex }, function(err) {
        getData(req, res);
    })
})
app.get("/change", function(req, res) {
    var id = req.query.id;
    var newTd = req.query.newTd;
    start.update({ _id: id }, { finish: newTd }, function(err) {
        getData(req, res);
    })
})
app.listen(8888);
console.log("开始运行服务器");